import tui from '../../../common/httpRequest'
const db = wx.cloud.database()

Page({
    data: {
        windowWidth: 375,
        windowHeight: 667,
        position: {
            left: 44,
            bottom: 54
        },
        productList: [],
        num: 0,
        pageIndex: 1,
        loadding: false,
        pullUpOn: true,
        content: '本项目唯一收入来源，请宝宝支持一下'
    },

    onLoad: function (options) {
        db.collection('productList').get().then(res => {
            console.log(res)

            this.setData({
                productList: res.data
            })
            console.log(res)

        })
        setTimeout(() => {
            let sys = wx.getSystemInfoSync();
            this.setData({
                windowWidth: sys.windowWidth,
                windowHeight: sys.windowHeight
            })
        }, 20);
    },
    addCart(e) {
        var cartList = wx.getStorageSync('cartList') || []
        // 获取本地缓存的购物车商品列表
        var index = e.detail.index
        console.log(cartList)
        // var cartList = wx.getStorageSync('cartList')
        // console.log(this.data.productList[index].id)
        let temp = this.data.productList[index]
        //构建购物车中商品对象
        var tempGoods = {
            id: temp.id,
            name: temp.name,
            price: temp.sale,
            stock: temp.stock,
            url: temp.url,
            selected: false,
            buyNum: 1
        }

        // 如果该商品已经在cart中，则尽改变数量
        let inde = cartList.findIndex(item => {
            return item.id == this.data.productList[index].id
        })
        if (inde === -1) {
            wx.setStorageSync('cartList', [tempGoods, ...cartList])
            this.UpdateCart();

        } else {
            cartList[inde].buyNum += 1;
            wx.setStorageSync('cartList', cartList);
            this.UpdateCart();
        }
    },

    UpdateCart: function () {
        var cart = wx.getStorageSync('cartList')
        let num = 0;
        if (cart) {
            for (var i = 0; i < cart.length; i++) {
                var goodsNumber = cart[i].buyNum;
                num += goodsNumber
            }
        }
        this.setData({
            num: num
        })
    },

    cart: function (e) {
        tui.href('../cart/cart')
    },
    order: function (e) {
        tui.href('../order/order')
    },

    search(e) {
        var that = this;
        that.setData({
            Text: e.detail.value
        });
        const arr = that.data.productList.filter(
            el => el.name.includes(that.data.Text)
        );
        that.data.productList = arr;
        that.setData({
            productList: that.data.productList
        });
        tui.toast('检索成功：' + e.detail.value)
    },

    onShow: function () {
        this.UpdateCart();
    },
    onPullDownRefresh: function () {

        // 显示顶部刷新图标
        wx.showNavigationBarLoading();
        db.collection('productList').get().then(res => {
            this.setData({
                productList: res.data
            })
        })
    },

    onShareAppMessage: function () {
        return {
            title: "顺易通食品专卖店"
        }
    }
})